//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    hasEmptyGrid: false,
    cur_year: '',
    cur_month: '',
    todayIndex:1, 
    array:[],
  },
  onLoad(options) {
    this.setNowDate();
    
  },

  dateSelectAction: function (e) {
    
    var cur_day = e.currentTarget.dataset.idx;
    if (this.data.array[0] > cur_day) {
      return;
    }
    if(this.data.array[0]==cur_day){
     for(var i=0;i<this.data.days.length;i++)
     {
       this.data.days[i].isSelected=false;
       this.data.days[i].status="住房";
     }
     this.setData({
       days:this.data.days,
       array:[]
     })
     return;
    }
    let arr = this.data.days[cur_day];
    arr.isSelected = !arr.isSelected;
    if(this.data.array.length==0){
      this.data.array.push(cur_day);
    }else if(this.data.array.length>0){
      
      for (var i = 0; i < this.data.array.length; i++) {
          this.data.array.push(cur_day);
          if(this.data.array.length>2){
         
            this.data.days[this.data.array[1]].isSelected = false;
            this.data.days[this.data.array[1]].status = "住房";
            this.data.array.splice(1, 1);
          }
          if (this.data.array.length == 2) {
            let arr = this.data.days[this.data.array[1]];
            arr.isSelected = !arr.isSelected;
            arr.status = "退房";
          }

      }
    }
    console.log(this.data.array);
    this.setData({
      days:this.data.days
    })
    console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day + 1}日`);

  },
  setNowDate: function () {
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const todayIndex = date.getDate() - 1;
    console.log(`日期：${todayIndex}`)
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    this.calculateEmptyGrids(cur_year, cur_month);
    this.calculateDays(cur_year, cur_month);
    this.setData({
      cur_year: cur_year,
      cur_month: cur_month,
      weeks_ch,
      todayIndex,
    })
  },

  getThisMonthDays(year, month) {
    return new Date(year, month, 0).getDate();
  },
  getFirstDayOfWeek(year, month) {
    return new Date(Date.UTC(year, month - 1, 1)).getDay();
  },
  calculateEmptyGrids(year, month) {
    const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
    let empytGrids = [];
    if (firstDayOfWeek > 0) {
      for (let i = 0; i < firstDayOfWeek; i++) {
        empytGrids.push(i);
      }
      
      this.setData({
        hasEmptyGrid: true,
        empytGrids
      });
      
    } else {
      this.setData({
        hasEmptyGrid: false,
        empytGrids: []
      });
      
    }
  },
  calculateDays(year, month) {
    let days = [];

    const thisMonthDays = this.getThisMonthDays(year, month);

    for (let i = 1; i <= thisMonthDays; i++) {
      days.push(i);
    }

    this.setData({
      days
    });
    let array = [];
    for (var i = 0; i < this.data.days.length; i++) {
      var time = this.data.days[i];
      array.push({ isSelected: false, time: time, status: '入住' });
    }

    this.setData({
      days: array
    })
  },
  handleCalendar(e) {
    const handle = e.currentTarget.dataset.handle;
    const cur_year = this.data.cur_year;
    const cur_month = this.data.cur_month;
    if (handle === 'prev') {
      let newMonth = cur_month - 1;
      let newYear = cur_year;
      if (newMonth < 1) {
        newYear = cur_year - 1;
        newMonth = 12;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })

    } else {
      let newMonth = cur_month + 1;
      let newYear = cur_year;
      if (newMonth > 12) {
        newYear = cur_year + 1;
        newMonth = 1;
      }

      this.calculateDays(newYear, newMonth);
      this.calculateEmptyGrids(newYear, newMonth);

      this.setData({
        cur_year: newYear,
        cur_month: newMonth
      })
    }
  }

})
